package com.zmn.plat.dubbo.impl.extensioncategory;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.extensioncategory.ExtensionCategoryDRO;
import com.zmn.plat.dubbo.interfaces.extensioncategory.ExtensionCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.extension.category.ExtensionCategory;
import com.zmn.plat.model.entity.extension.category.ExtensionCategoryQuery;
import com.zmn.plat.services.interfaces.extension.category.ExtensionCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;

/**
 * 描述：推广分类dubbo接口实现
 *
 * @author linfeng
 * @version v1.0
 * @since 2020/03/30 10:42
 */
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ExtensionCategoryListRemoteServiceImpl implements ExtensionCategoryListRemoteService {
    private static final String TAG = "推广分类 dubbo 接口";

    @Resource
    ExtensionCategoryService extensionCategoryService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<ExtensionCategoryDRO> getByExtCategId(Integer id) {
        log.info("ExtensionCategoryListRemoteServiceImpl.getByExtCategId 参数为：id:{}", id);
        if (Objects.isNull(id)) {
            return ResponseDTO.fail("id不能为空");
        }
        ExtensionCategory extensionCategory = extensionCategoryService.findByKey(id);
        if (Objects.isNull(extensionCategory)) {
            return ResponseDTO.fail("数据不存在");
        }
        ExtensionCategoryDRO dro = new ExtensionCategoryDRO();
        try {
            BeanCopier copierDro = BeanCopier.create(ExtensionCategory.class, ExtensionCategoryDRO.class, false);
            copierDro.copy(extensionCategory, dro, null);
        } catch (Exception ex) {
            log.error("{}对象:{}拷贝出错", TAG, "CopyObjectUtil<ExtensionCategory, ExtensionCategoryDRO>", ex);
            return ResponseDTO.fail("拷贝ExtensionCategory对象出错");
        }

        return ResponseDTO.success(dro, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listByNameAndStatus(String name, Integer status) {
        log.info("ExtensionCategoryListRemoteServiceImpl.listByNameAndStatus 参数为：name:{},status:{}", name, status);
        ExtensionCategoryQuery extensionCategoryQuery = new ExtensionCategoryQuery();
        extensionCategoryQuery.setName(name);
        extensionCategoryQuery.setStatus(status);
        List<ExtensionCategory> extensionCategories = extensionCategoryService.listByQuery(extensionCategoryQuery);

        if (isEmpty(extensionCategories)) {
            return ResponseDTO.success(emptyList(), "无符合条件的数据");
        }

        List<VtDTO> vtList = extensionCategories.stream()
            .map(extensionCategory -> new VtDTO(extensionCategory.getCategId(), extensionCategory.getName(), extensionCategory.getStatus()))
            .collect(toList());

        return ResponseDTO.success(vtList, GlobalConsts.SUCCESS_MSG);
    }
}
